<?php
/*
Template Name: Gallery
*/

get_header();

$height=ThemexCore::getOption('gallery_height','0');
$layout=ThemexCore::getOption('galleries_layout','three');
$row_limit=$layout=='four'?3:4;
$page_limit=ThemexCore::getOption('galleries_limit','12');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$category=is_null($term=get_term( get_post_meta($post->ID,'_page_galleries_category', true), 'gallery_category' )) || is_wp_error($term)? null : $term->slug;

query_posts(array(
	'post_type' => 'gallery',
	'gallery_category' => $category,
	'posts_per_page' => intval($page_limit),
	'paged' => $paged,
	'meta_key' => '_thumbnail_id'
));
?>
<div class="items-grid">
	<?php 
		$counter=0;
		if (have_posts()) {
			while ( have_posts() ) {
				the_post();
				$counter++;
				parse_str(get_post_meta($post->ID,'_gallery_images',true),$gallery);
				$permalink=wp_get_attachment_url(get_post_thumbnail_id($post->ID));				
			?>
			<div class="column gallery-item <?php echo $layout; ?>col <?php echo $counter==$row_limit ? 'last' : ''; ?>">
				<div class="featured-image">
					<a href="<?php echo $permalink; ?>" class="fancybox" data-group="gallery-<?php echo $post->ID; ?>">
						<img src="<?php echo themex_thumbnail($post->ID,460,intval($height)); ?>" class="fullwidth" alt="<?php the_title(); ?>" />
					</a>
					<a class="featured-image-caption visible-caption" href="#">
						<h6><?php the_title(); ?></h6>
					</a>					
				</div>
				<div class="hidden">
					<?php foreach($gallery as $image) { ?>
					<a class="fancybox" href="<?php echo $image; ?>" data-group="gallery-<?php echo $post->ID; ?>"></a>
					<?php } ?>
				</div>
				<div class="block-background"></div>
			</div>
			<?php
				if($counter==$row_limit) {
					$counter=0;
					echo '<div class="clear"></div>';
				}
			}
		}
	?>
	<div class="clear"></div>
</div><!--/ items grid-->
<?php 
themex_pagination(); 
wp_reset_query();
?>
<?php get_footer(); ?>